﻿using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Rendering;

namespace Super_Management_Mayhem.Screens.Playing.Players
{
    /// <summary>
    /// Class used to contain the player and draws it with a custom shader
    /// </summary>
    public class Character : ImageContainer
    {
        private static int _characterId = 0;
        private readonly string _shaderId;

        private const bool SHADERS_ENABLED = true;

        public Character(Color skinColor)
        {
            //If commented out, it's because I'm developing on a machine that doesn't support OpenGL shaders!

            if (SHADERS_ENABLED)
            {
                _shaderId = "colorReplaceShader-" + _characterId++;

                ShaderHandler.AddShader(_shaderId, null, "Content/Shaders/ColorChange.frag");

                ShaderHandler.SetParameter(_shaderId, "sourceColor", new Color(255, 0, 255));
                ShaderHandler.SetParameter(_shaderId, "targetColor", skinColor);
                ShaderHandler.SetParameter(_shaderId, "tolerance", 0.1f);
            }
            
        }

        public override void Draw()
        {
            if (SHADERS_ENABLED)
            {
                ShaderHandler.SetParameter(_shaderId, "maskColor", Color);
                ShaderHandler.SetShader(_shaderId);

                base.Draw();
                ShaderHandler.RemoveShader();
            }
            else
            {
                base.Draw();
            }
            
        }

        public void SetColor(Color color)
        {
            if(SHADERS_ENABLED)
                ShaderHandler.SetParameter(_shaderId, "targetColor", color);
        }
    }
}
